package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CouponModel;
import com.rockcent.model.CouponStore;
import com.rockcent.model.constant.BooleanType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.fragment.EvaluateFragment;
import com.xysq.fragment.GoodsFragment;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class CommodityDetailsActivity extends BaseActivity implements EvaluateFragment.OnFragmentInteractionListener, GoodsFragment.OnFragmentInteractionListener, View.OnClickListener {
    private List<Fragment> fragmentList;
    private Button buyNowBtn;
    private TextView fragmentGoodsTxt;
    private TextView fragmentEvaluateTxt;
    private ImageView fragmentGoodsImg;
    private ImageView fragmentEvaluateImg;
    private ImageView backBtn;
    private int couponModelId;
    private boolean isFavorite;
    FragmentPagerAdapter adapter;
    ViewPager pager;
    @InjectView(R.id.img_love_or_not)
    ImageView loveOrNotImg;
    private LinearLayout saveLayout;
    private LinearLayout connectsellerLayout;
    private RelativeLayout goodsIndicaterLayout;
    private RelativeLayout evaluateIndicaterLayout;
    public static final String COUPONMODEL_ID = "couponModel_Id";
    public static final String CHANNEL_ID = "channel_Id";
    public static final String CUSTOMER_ID = "customer_id";
    public static final String TYPE = "type";

    @Override
    protected void onPause() {
        MobclickAgent.onPause(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        MobclickAgent.onResume(this);
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_details);
        initView();
        loadData();
        processFragmentList();
    }

    private void loadData() {
        getIsFavorite();
    }

    private void getIsFavorite() {
        appAction.viewCouponModel(couponModelId, UserInfoKeeper.readPhoneNumber(CommodityDetailsActivity.this), 0, new CallbackListener<CouponModel>() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onSuccess(CouponModel data) {
                if (data.getIsFavorite() == BooleanType.TRUE) {
                    isFavorite = true;
                    loveOrNotImg.setBackground(getResources().getDrawable(R.drawable.ic_love_solid_big));
                } else if (data.getIsFavorite() == BooleanType.FALSE) {
                    isFavorite = false;
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(CommodityDetailsActivity.this, "获取收藏信息失败");
                Log.e("获取收藏信息失败的原因", message);
            }
        });
    }

    private void processFragmentList() {
        fragmentList = new ArrayList<>();
        fragmentList.add(GoodsFragment.newInstance(couponModelId));
        fragmentList.add(EvaluateFragment.newInstance(couponModelId));
        adapter = new GoogleMusicAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    fragmentGoodsTxt.setTextColor(Color.RED);
                    fragmentEvaluateTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentGoodsImg.setBackgroundColor(Color.RED);
                    fragmentEvaluateImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                }
                if (i == 1) {
                    fragmentGoodsTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentEvaluateTxt.setTextColor(Color.RED);
                    fragmentGoodsImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                    fragmentEvaluateImg.setBackgroundColor(Color.RED);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void initView() {
        goodsIndicaterLayout = (RelativeLayout) findViewById(R.id.layout_goods_indicater);
        evaluateIndicaterLayout = (RelativeLayout) findViewById(R.id.layout_evaluate_indicater);
        saveLayout = (LinearLayout) findViewById(R.id.layout_save);
        connectsellerLayout = (LinearLayout) findViewById(R.id.layout_connect_seller);
        backBtn = (ImageView) findViewById(R.id.img_back);
        goodsIndicaterLayout.setOnClickListener(this);
        evaluateIndicaterLayout.setOnClickListener(this);
        saveLayout.setOnClickListener(this);
        connectsellerLayout.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        couponModelId = getIntent().getIntExtra(COUPONMODEL_ID, 0);
        buyNowBtn = (Button) findViewById(R.id.btn_buy_now);
        if(getIntent().getStringExtra(TYPE) != null) {//白送
            buyNowBtn.setEnabled(false);
        }
        fragmentEvaluateImg = (ImageView) findViewById(R.id.img_fragment_evaluate);
        fragmentGoodsImg = (ImageView) findViewById(R.id.img_fragment_goods);
        fragmentEvaluateTxt = (TextView) findViewById(R.id.txt_fragment_evaluate);
        fragmentGoodsTxt = (TextView) findViewById(R.id.txt_fragment_goods);
        fragmentGoodsTxt.setTextColor(getResources().getColor(R.color.text_red));
        fragmentGoodsImg.setBackgroundColor(getResources().getColor(R.color.text_red));
        buyNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobclickAgent.onEvent(CommodityDetailsActivity.this, "1007");
                if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(CommodityDetailsActivity.this))) {
                    startActivity(new Intent(CommodityDetailsActivity.this, LoginActivity.class));
                } else {
                    Intent intent = new Intent(CommodityDetailsActivity.this, PayActivity.class);
                    intent.putExtra(PayActivity.COUPON_MODEL_ID, Integer.valueOf(couponModelId));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onClick(View view) {
        if (view == goodsIndicaterLayout) {
            pager.setCurrentItem(0);
        }
        if (view == evaluateIndicaterLayout) {
            pager.setCurrentItem(1);
        }
        if (view == backBtn) {
            finish();
        }
        if (view == saveLayout) {
            if(TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(CommodityDetailsActivity.this))){
                startActivity(new Intent(this, LoginActivity.class));
            }
            else {
                saveGoods();
            }
        }
        if (view == connectsellerLayout) {
            MobclickAgent.onEvent(CommodityDetailsActivity.this, "1009");
            appAction.listCouponStore(couponModelId, new CallbackListener<List<CouponStore>>() {
                @Override
                public void onSuccess(List<CouponStore> data) {
                    String tel = data.get(0).getTel();
                    Uri smsToUri = Uri.parse("smsto:"+tel);
                    Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                    startActivity(intent);
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showShort(CommodityDetailsActivity.this, "获取商家电话失败");
                }
            });
        }
    }

    private void saveGoods() {
        if (isFavorite == true) {
            ToastUtil.showShort(CommodityDetailsActivity.this, "您已经收藏过了亲");
        } else if (isFavorite == false) {
            appAction.addFavorite(UserInfoKeeper.readPhoneNumber(CommodityDetailsActivity.this), couponModelId, new CallbackListener<Void>() {
                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onSuccess(Void data) {
                    ToastUtil.showShort(CommodityDetailsActivity.this, "收藏成功");
                    loveOrNotImg.setBackground(getResources().getDrawable(R.drawable.ic_love_solid_big));
                    isFavorite = true;
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showShort(CommodityDetailsActivity.this, "收藏失败");
                    Log.e("+收藏失败的原因", message);
                }
            });
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("test", "stop");
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return getResources().getString(R.string.txt_goods);
            }
            if (position == 1) {
                return getResources().getString(R.string.txt_evaluate);
            }
            return null;
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
